package org.gruposp2p.dnie.service.jpa;

import java.util.List;
import javax.ws.rs.core.Context;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import org.gruposp2p.dnie.model.DNIeUser;
import org.gruposp2p.dnie.service.DNIeUserDAO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;


/**
 *
 * @author jj
 */
@Repository("userDao")
public class JpaDNIeUserDAO implements DNIeUserDAO {

    private static Logger logger = LoggerFactory.getLogger(JpaDNIeUserDAO.class);

    @Context HttpServletRequest request;
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
    }
    
    public DNIeUser findById(Integer id) {
        return entityManager.find(DNIeUser.class, id);
    }

    public List<DNIeUser> findAll() {
        Query query = 
                entityManager.createQuery("select d from DNIeUser d");
        List<DNIeUser> users = null;
        users = query.getResultList();
        return users;
    }

    public DNIeUser findByNif(String nif) {
    	DNIeUser result = null;
        Query query =
                entityManager.createQuery("SELECT d FROM DNIeUser d WHERE d.nif = :nif");
        query.setParameter("nif", nif);
        List<DNIeUser> users = query.getResultList();
        if (!users.isEmpty()) result = users.get(0);
        return result;
    }

    public List<DNIeUser> findByPartialName(String partName) {
        Query query =
                entityManager.createQuery("select c from DNIeUser c where c.name LIKE ?1");
        List<DNIeUser> users = null;
        users = query.getResultList();
        return users;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public DNIeUser save(DNIeUser user) {
        entityManager.persist(user);
        entityManager.flush();
        return user;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public DNIeUser update(DNIeUser user) {
        entityManager.merge(user);
        entityManager.flush();
        return user;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public boolean delete(DNIeUser user) {
        user = entityManager.getReference(DNIeUser.class, user.getId());
        if (user == null)
                return false;
        entityManager.remove(user);
        entityManager.flush();
        return true;
    }

    public List<DNIeUser> findList(int firstResult, int maxResults) {
        Query query = entityManager.createQuery(
                "SELECT d FROM DNIeUser d ORDER BY dateAdded DESC");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<DNIeUser> users = query.getResultList();
        return users;
    }

}
